﻿// <copyright file="MsdnXmlHandlerTest.cs" company="Snowman Consulting">
//     Copyright (c) 2009 Snowman Consulting. All rights reserved.
// </copyright>

namespace SnowmanConsulting.ProductKeyManager.Test
{
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using SnowmanConsulting.ProductKeyManager.Data;
    using SnowmanConsulting.ProductKeyManager.Xml;
    
    /// <summary>
    /// This is a test class for MsdnXmlHandlerTest and is intended
    /// to contain all MsdnXmlHandlerTest Unit Tests
    /// </summary>
    [TestClass()]
    public class MsdnXmlHandlerTest
    {
        /// <summary>
        /// Test Context
        /// </summary>
        private TestContext testContextInstance;

        /// <summary>
        /// Gets or sets the test context which provides
        /// information about and functionality for the current test run.
        /// </summary>
        public TestContext TestContext
        {
            get
            {
                return this.testContextInstance;
            }

            set
            {
                this.testContextInstance = value;
            }
        }

        /// <summary>
        /// A test for MsdnXmlHandler Constructor
        /// </summary>
        [TestMethod()]
        public void MsdnXmlHandlerConstructorTest()
        {
            MsdnXmlHandler target = new MsdnXmlHandler();
            Assert.IsNotNull(target);
        }

        /// <summary>
        /// Imports the export test.
        /// </summary>
        [TestMethod]
        public void ImportExportTest()
        {
            MsdnXmlHandler exportHandler = new MsdnXmlHandler();

            ProductCollection expectedProducts = new ProductCollection();
            TestHelper.CreateExpectedProducts(expectedProducts);

            exportHandler.GenerateXml(expectedProducts);

            Assert.IsFalse(string.IsNullOrEmpty(exportHandler.GeneratedXml));

            MsdnXmlHandler importHandler = new MsdnXmlHandler();
            importHandler.Load(exportHandler.GeneratedXml);

            Product[] keylessProducts = (from kp in expectedProducts where kp.Keys.Count == 0 select kp).ToArray();
            
            foreach (Product keylessProduct in keylessProducts)
            {
                expectedProducts.Remove(keylessProduct);
            }

            ProductCollection actualProducts = importHandler.Products;

            Assert.IsNotNull(actualProducts);
            Assert.AreEqual(expectedProducts.Count, actualProducts.Count);

            for (int i = 0; i < actualProducts.Count; i++)
            {
                Assert.AreEqual(expectedProducts[i].Name, actualProducts[i].Name, false);
                Assert.AreEqual(expectedProducts[i].Category, actualProducts[i].Category, false);
                Assert.IsNull(actualProducts[i].Icon);
                Assert.AreEqual(expectedProducts[i].Notes, actualProducts[i].Notes, false);

                Assert.AreEqual(expectedProducts[i].Keys.Count, actualProducts[i].Keys.Count);

                for (int j = 0; j < actualProducts[i].Keys.Count; j++)
                {
                    Assert.AreEqual(expectedProducts[i].Keys[j].Code, actualProducts[i].Keys[j].Code, false);
                    Assert.AreNotEqual(expectedProducts[i].Keys[j].Notes, actualProducts[i].Keys[j].Notes, false);
                    if (!string.IsNullOrEmpty(expectedProducts[i].Keys[j].AllocatedTo))
                    {
                        Assert.AreEqual(expectedProducts[i].Keys[j].AllocatedTo, actualProducts[i].Keys[j].AllocatedTo, false);
                    }
                    else
                    {
                        Assert.AreEqual("Not Allocated", actualProducts[i].Keys[j].AllocatedTo, false);
                    }
                }
            }
        }
    }
}
